package com.doo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.util.*;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class HelloController {

    @Value("${app.basedir}")
    private String dir;

    public String addSuffix(String fileName) {
        // 创建文件对象
        File oldFile = new File(fileName);

        // 新文件名
        String newName = fileName.replace("pc","png");

        System.out.println(newName);

        // 使用renameTo()方法来修改文件名
        if (oldFile.renameTo(new File(newName))) {
            System.out.println("文件名已成功修改！");
        } else {
            System.out.println("文件名修改失败！");
        }

        return newName;
    }

    public String removeSuffix(String fileName) {
        // 创建文件对象
        File oldFile = new File(fileName);

        // 新文件名
        String newName = fileName.split("\\.")[0];

        // 使用renameTo()方法来修改文件名
        if (oldFile.renameTo(new File(newName))) {
            System.out.println("文件名已成功修改！");
        } else {
            System.out.println("文件名修改失败！");
        }

        return newName;
    }

    @GetMapping("/download")
    public ResponseEntity<Resource> download(@RequestParam String fileName) throws Exception {
        Path path = Paths.get(dir, fileName);
        Resource resource = new UrlResource(path.toUri());

        if (resource.exists() || resource.isReadable()) {
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"");
            return ResponseEntity.ok()
                    .headers(headers)
                    .contentLength(resource.contentLength())
                    .contentType(MediaType.IMAGE_JPEG)
                    .body(resource);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/")
    public String test(Model model) {
        File file = new File(dir);
        File[] files = file.listFiles();

        List<String> arrList = new ArrayList<>();

        if (files != null && files.length > 0) {
            for (File item : files) {
                String name = item.getName();
                String[] arr = name.split("\\.");
                String suffix = arr[1];

                if (!item.isDirectory() && !suffix.equals("zip")) {
//                if (suffix.equals("pc")) {
                    arrList.add("/download?fileName=" + item.getName());
                }
            }
        }

        model.addAttribute("list", arrList);

        return "/box/demo/index";
    }
}